package com.ruoyi.order.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.MesPackageProduct;
import com.ruoyi.order.domain.MesTopOrder;
import com.ruoyi.order.proxy.factory.FactoryProxy;
import com.ruoyi.order.proxy.factory.req.PackBoxDataRequest;
import com.ruoyi.order.service.IMesPackageProductService;
import com.ruoyi.order.service.IMesTopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.MesPackageMapper;
import com.ruoyi.order.domain.MesPackage;
import com.ruoyi.order.service.IMesPackageService;

/**
 * 产品装箱Service业务层处理
 * 
 * @author guoxl
 * @date 2025-08-05
 */
@Service
public class MesPackageServiceImpl implements IMesPackageService 
{
    @Autowired
    private MesPackageMapper mesPackageMapper;

    @Autowired
    private IMesPackageProductService mesPackageProductService;

    @Autowired
    private FactoryProxy factoryProxy;

    @Autowired
    private IMesTopOrderService mesTopOrderService;

    /**
     * 查询产品装箱
     * 
     * @param packId 产品装箱主键
     * @return 产品装箱
     */
    @Override
    public MesPackage selectMesPackageByPackId(Long packId)
    {
        MesPackage mesPackage = mesPackageMapper.selectMesPackageByPackId(packId);
        if (mesPackage != null) {
            MesPackageProduct mesPackageProduct = new MesPackageProduct();
            mesPackageProduct.setPackCode(mesPackage.getPackCode());
            List<MesPackageProduct> packageProducts = mesPackageProductService.selectMesPackageProductList(mesPackageProduct);
            if (packageProducts != null) {
                String[] partIdentifiers = new String[packageProducts.size()];
                for (int i = 0; i < packageProducts.size(); i++) {
                    partIdentifiers[i] = packageProducts.get(i).getPartIdentifier();
                }
                mesPackage.setPartIdentifiers(partIdentifiers);
            }
         }
        return mesPackage;
    }

    /**
     * 查询产品装箱列表
     * 
     * @param mesPackage 产品装箱
     * @return 产品装箱
     */
    @Override
    public List<MesPackage> selectMesPackageList(MesPackage mesPackage)
    {
        if (mesPackage.getPartIdentifier() != null){
            MesPackageProduct param = new MesPackageProduct();
            param.setPartIdentifier(mesPackage.getPartIdentifier());
            List<MesPackageProduct> packageProducts = mesPackageProductService.selectMesPackageProductList(param);
            if (packageProducts != null){
                mesPackage.setPackCode(packageProducts.get(0).getPackCode());
            }
        }
        return mesPackageMapper.selectMesPackageList(mesPackage);
    }

    /**
     * 新增产品装箱
     * 
     * @param mesPackage 产品装箱
     * @return 结果
     */
    @Override
    public int insertMesPackage(MesPackage mesPackage)
    {
        MesTopOrder mesTopOrder = mesTopOrderService.selectMesTopOrderProducting();
        if (mesTopOrder != null){
            mesPackage.setOrderNumber(mesTopOrder.getOrderNumber());
        }
        mesPackage.setCreateTime(DateUtils.getNowDate());
        mesPackage.setStatus("1");
        for (String partIdentifier : mesPackage.getPartIdentifiers()) {
            MesPackageProduct mesPackageProduct = new MesPackageProduct();
            mesPackageProduct.setPartIdentifier(partIdentifier);
            mesPackageProduct.setPackCode(mesPackage.getPackCode());
            mesPackageProduct.setOrderNumber(mesPackage.getOrderNumber());
            mesPackageProduct.setCreateTime(DateUtils.getNowDate());
            mesPackageProductService.insertMesPackageProduct(mesPackageProduct);
        }
        return mesPackageMapper.insertMesPackage(mesPackage);
    }

    /**
     * 修改产品装箱
     * 
     * @param mesPackage 产品装箱
     * @return 结果
     */
    @Override
    public int updateMesPackage(MesPackage mesPackage)
    {
        mesPackage.setUpdateTime(DateUtils.getNowDate());
        mesPackageProductService.deleteMesPackageProductByPackCode(mesPackage.getPackCode());
        for (String partIdentifier : mesPackage.getPartIdentifiers()) {
            MesPackageProduct mesPackageProduct = new MesPackageProduct();
            mesPackageProduct.setPartIdentifier(partIdentifier);
            mesPackageProduct.setPackCode(mesPackage.getPackCode());
            mesPackageProduct.setOrderNumber(mesPackage.getOrderNumber());
            mesPackageProduct.setCreateTime(DateUtils.getNowDate());
            mesPackageProductService.insertMesPackageProduct(mesPackageProduct);
        }
        return mesPackageMapper.updateMesPackage(mesPackage);
    }

    /**
     * 批量删除产品装箱
     * 
     * @param packIds 需要删除的产品装箱主键
     * @return 结果
     */
    @Override
    public int deleteMesPackageByPackIds(Long[] packIds)
    {
        for (Long packId : packIds){
            MesPackage mesPackage = selectMesPackageByPackId(packId);
            mesPackageProductService.deleteMesPackageProductByPackCode(mesPackage.getPackCode());
        }
        return mesPackageMapper.deleteMesPackageByPackIds(packIds);
    }

    /**
     * 批量删除产品装箱
     *
     * @param packIds 需要删除的产品装箱主键
     * @param urlPrefix 推送地址前缀
     * @return 结果
     */
    @Override
    public int sendPackageByPackIds(Long[] packIds, String urlPrefix)
    {
        for (Long packId : packIds){
            MesPackage mesPackage = selectMesPackageByPackId(packId);
            List<MesPackageProduct> packageProducts = mesPackageProductService.selectListByPackCode(mesPackage.getPackCode());
            if (packageProducts != null) {
                String[] partIdentifiers = new String[packageProducts.size()];
                for (int i = 0; i < packageProducts.size(); i++) {
                    partIdentifiers[i] = packageProducts.get(i).getPartIdentifier();
                }
                PackBoxDataRequest request = new PackBoxDataRequest();
                request.setBoxCode(mesPackage.getPackCode());
                request.setPartIdentifierList(partIdentifiers);
                //factoryProxy.packBoxData(request, urlPrefix);
                mesPackage.setStatus("2");
                mesPackageMapper.updateMesPackage(mesPackage);
            }
        }
        return 1;
    }

    /**
     * 删除产品装箱信息
     * 
     * @param packId 产品装箱主键
     * @return 结果
     */
    @Override
    public int deleteMesPackageByPackId(Long packId)
    {
        return mesPackageMapper.deleteMesPackageByPackId(packId);
    }
}
